package com.qf.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsStorage;
import com.qf.service.DtsStorageService;
import com.qf.util.AuthSupport;
import com.qf.util.ResponseUtil;
import org.apache.commons.io.FilenameUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对象存储
 * @author lixu
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/storage")
public class StorageController {

    @Autowired
    private DtsStorageService dtsStorageService;

    /**
     * 上传图片
     */
    @PostMapping("/create")
    public Object create(MultipartFile file) throws Exception{
        String ext = FilenameUtils.getExtension(file.getOriginalFilename());
        //上传
        String path = dtsStorageService.uploadFile(
                file.getBytes(),ext);
        Map<String,Object> data = new HashMap<>();
        data.put("url",path);
        return ResponseUtil.ok(data);
    }


    /**
     * 对象存储列表查询
     */
    @RequiresPermissions("admin:storage:list")
    @RequiresPermissionsDesc(menu = { "系统管理", "对象存储" }, button = "查询")
    @GetMapping("/list")
    public Object list(String key, String name, @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @RequestParam(defaultValue = "add_time") String sort,
                       @RequestParam(defaultValue = "desc") String order) {

        IPage<DtsStorage> iPage = dtsStorageService.querySelective(key, name, page, limit, sort, order);
        long total = iPage.getTotal();
        List<DtsStorage> list = iPage.getRecords();
        Map<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", list);

        return ResponseUtil.ok(data);
    }
    /**
     * 更新文件
     * @param dtsStorage
     * @return
     */
    @RequiresPermissions("admin:storage:update")
    @RequiresPermissionsDesc(menu = { "系统管理", "对象存储" }, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody DtsStorage dtsStorage) {

        if (dtsStorageService.update(dtsStorage) == 0) {
            return ResponseUtil.updatedDataFailed();
        }

        return ResponseUtil.ok(dtsStorage);
    }

    /**
     * 删除文件
     * @param DtsStorage
     * @return
     */
    @RequiresPermissions("admin:storage:delete")
    @RequiresPermissionsDesc(menu = { "系统管理", "对象存储" }, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsStorage DtsStorage) {

        String key = DtsStorage.getKey();
        if (StringUtils.isEmpty(key)) {
            return ResponseUtil.badArgument();
        }
        dtsStorageService.deleteByKey(key);

        return ResponseUtil.ok();
    }
}
